// This do-file extracts data on HDI from the UNDP

** Importing data

// HDI
import delimited "$project_path/data/1_input/outcomes/hdi/hdi.csv", encoding(UTF-8) varnames(6) rowrange(6) clear 
local i = 1
local y = 1989
forvalues j = 1/30 {
	local i = `i'+2
	local y = `y'+1
	ren v`i' hdi`y' 
}
ren country Country
keep Country hdi*
drop hdirank
reshape long hdi, i(Country) j(Year)
replace hdi = "" if hdi==".."
destring hdi, replace
replace Country = substr(Country,2,.) if substr(Country,1,1)==" "
drop if Country==""
tempfile hdi
save `hdi'

// Life expectancy component
import delimited "$project_path/data/1_input/outcomes/hdi/life_exp_index.csv", encoding(UTF-8) varnames(6) rowrange(6) clear 
local i = 1
local y = 1989
forvalues j = 1/30 {
	local i = `i'+2
	local y = `y'+1
	cap replace v`i' = "" if v`i'==".."
	ren v`i' lifeexp_index`y' 
}
ren country Country
keep Country lifeexp_index*
destring lifeexp_index*, replace
reshape long lifeexp_index, i(Country) j(Year)
replace Country = substr(Country,2,.) if substr(Country,1,1)==" "
drop if Country==""
tempfile lifeexp_index
save `lifeexp_index'

// Education component
import delimited "$project_path/data/1_input/outcomes/hdi/education_index.csv", encoding(UTF-8) varnames(6) rowrange(6) clear 
local i = 1
local y = 1989
forvalues j = 1/30 {
	local i = `i'+2
	local y = `y'+1
	cap replace v`i' = "" if v`i'==".."
	ren v`i' educ_index`y' 
}
ren country Country
keep Country educ_index*
destring educ_index*, replace
reshape long educ_index, i(Country) j(Year)
replace Country = substr(Country,2,.) if substr(Country,1,1)==" "
drop if Country==""
tempfile educ_index
save `educ_index'

// Income component
import delimited "$project_path/data/1_input/outcomes/hdi/income_index.csv", encoding(UTF-8) varnames(6) rowrange(6) clear 
local i = 1
local y = 1989
forvalues j = 1/30 {
	local i = `i'+2
	local y = `y'+1
	cap replace v`i' = "" if v`i'==".."
	ren v`i' income_index`y' 
}
ren country Country
keep Country income_index*
destring income_index*, replace
reshape long income_index, i(Country) j(Year)
replace Country = substr(Country,2,.) if substr(Country,1,1)==" "
drop if Country==""
tempfile income_index
save `income_index'

** Gathering variables, adjusting country names, and exporting output dataset

use `hdi', clear
merge 1:1 Country Year using `lifeexp_index', nogen
merge 1:1 Country Year using `educ_index', nogen
merge 1:1 Country Year using `income_index', nogen

replace Country="Bolivia"  if Country=="Bolivia (Plurinational State of)"
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Brunei"  if Country=="Brunei Darussalam"
replace Country="C. Verde Is."  if Country=="Cabo Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Congo (DRC)"  if Country=="Congo (Democratic Republic of the)"
replace Country="Czech Rep."  if Country=="Czechia"
replace Country="Ivory Coast"  if strpos(Country, "d'Ivoire")
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini (Kingdom of)"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="Iran"  if Country=="Iran (Islamic Republic of)"
replace Country="Korea, Republic of"  if Country=="Korea (Republic of)"
replace Country="PRK"  if Country=="Korea (Democratic People's Rep. of)"
replace Country="Laos"  if Country=="Lao People's Democratic Republic"
replace Country="Micronesia, Federated States of"  if Country=="Micronesia (Federated States of)"
replace Country="Moldova"  if Country=="Moldova (Republic of)"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="Occupied Palestinian Territories"  if Country=="Palestine, State of"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Russia"  if Country=="Russian Federation"
replace Country="St. Lucia"  if Country=="Saint Lucia"
replace Country="Saint Vincent and The Grenadines"  if Country=="Saint Vincent and the Grenadines"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Syria"  if Country=="Syrian Arab Republic"
replace Country="Tanzania"  if Country=="Tanzania (United Republic of)"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States"
replace Country="Venezuela"  if Country=="Venezuela (Bolivarian Republic of)"
replace Country="Vietnam"  if Country=="Viet Nam"
drop if Country=="Arab States"
drop if Country=="Developing Countries"
drop if Country=="East Asia and the Pacific"
drop if Country=="Europe and Central Asia"
drop if Country=="High human development"
drop if Country=="Hong Kong, China (SAR)"
drop if Country=="Human Development"
drop if Country=="Latin America and the Caribbean"
drop if Country=="Least Developed Countries"
drop if Country=="Low human development"
drop if Country=="Medium human development"
drop if Country=="Organization for Economic Co-operation and Development"
drop if Country=="Regions"
drop if Country=="Small Island Developing States"
drop if Country=="South Asia"
drop if Country=="Sub-Saharan Africa"
drop if Country=="Very high human development"
drop if Country=="World"

lab var hdi "Human Development Index (HDI)"
lab var lifeexp_index "HDI: Life expectancy component"
lab var educ_index "HDI: Education component"
lab var income_index "HDI: Income component"
foreach v in hdi lifeexp_index educ_index income_index {
	notes `v' : Source - UNDP
}

save "$project_path/data/2_intermediary/outcomes/outcomes_undp.dta", replace
